package com.tangkun.environment.activity;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tangkun.environment.R;
import com.tangkun.environment.entity.gongzuo.CategoryTreeDataReturn;
import com.tangkun.environment.entity.shouye.EnterpriseViewReturn;
import com.tangkun.environment.network.MySubscribe;
import com.tangkun.environment.rx.RxActivityHelper;
import com.tangkun.environment.utils.JumpHelper;
import com.tangkun.environment.utils.LogUtils;

import org.simple.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 企业信息
 */
public class QiYeXinXiActivity extends BaseActivity {

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.tv_jichushuju_yingyezhizhao)
    TextView tvJichushujuYingyezhizhao;
    @BindView(R.id.tv_jichushuju_dizhi)
    TextView tvJichushujuDizhi;
    @BindView(R.id.ll_jichushuju_dizhi)
    LinearLayout llJichushujuDizhi;
    @BindView(R.id.tv_jichushuju_chanyexiehui)
    TextView tvJichushujuChanyexiehui;
    @BindView(R.id.tv_jichushuju_dianhua)
    TextView tvJichushujuDianhua;
    @BindView(R.id.ll_jichushuju_dianhua)
    LinearLayout llJichushujuDianhua;
    @BindView(R.id.tv_jichushuju_yingyehao)
    TextView tvJichushujuYingyehao;
    /**
     * ID
     */
    private String id = "";
    /**
     * 上下文
     */
    private Context mContext;
    /**
     * 经纬度
     */
    private String lat = "";
    /**
     * 经纬度
     */
    private String lng = "";
    /**
     * 地址
     */
    private String address = "";
    /**
     * 行业分类ID
     */
    private String categoryid = "";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qiyexinxi);
        id = getIntent().getStringExtra("id");
        mContext = this;
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        title.setText("企业信息");

        enterpriseView();
    }

    /**
     * 企业中心
     */
    private void enterpriseView() {
        fromNetwork.enterpriseView(id).compose(new RxActivityHelper<EnterpriseViewReturn>().ioMain(QiYeXinXiActivity.this, true))
                .subscribe(new MySubscribe<EnterpriseViewReturn>(mContext, true) {
                    @Override
                    protected void success(EnterpriseViewReturn resultBean) {
                        if (resultBean.data != null) {
                            //赋值经纬度地址，用于跳转定位页面
                            lat = resultBean.data.lat;
                            lng = resultBean.data.lng;
                            address = resultBean.data.address;

                            //单位名称
                            if (!TextUtils.isEmpty(resultBean.data.name)) {
                                tvJichushujuYingyezhizhao.setText(resultBean.data.name);
                            } else {
                                tvJichushujuYingyezhizhao.setText("");
                            }
                            //地址
                            if (!TextUtils.isEmpty(resultBean.data.address)) {
                                tvJichushujuDizhi.setText(resultBean.data.address);
                            } else {
                                tvJichushujuDizhi.setText("");
                            }
                            //联系人
                            if (!TextUtils.isEmpty(resultBean.data.legalname)) {
                                tvJichushujuChanyexiehui.setText(resultBean.data.legalname);
                            } else {
                                tvJichushujuChanyexiehui.setText("");
                            }
                            //电话
                            if (!TextUtils.isEmpty(resultBean.data.tel)) {
                                tvJichushujuDianhua.setText(resultBean.data.tel);
                            } else {
                                tvJichushujuDianhua.setText("");
                            }
                            //行业分类
                            categoryid = resultBean.data.categoryid;
                            //请求行业分类
                            categoryTreeData();
                        }
                    }

                    @Override
                    protected void onMyError(String errorMessage) {
                        showToast(errorMessage + "");
                    }
                });
    }

    /**
     * 行业分类
     */
    private void categoryTreeData() {
        fromNetwork.categoryTreeData().compose(new RxActivityHelper<CategoryTreeDataReturn>().ioMain(QiYeXinXiActivity.this, false))
                .subscribe(new MySubscribe<CategoryTreeDataReturn>(QiYeXinXiActivity.this, false) {
                    @Override
                    protected void success(CategoryTreeDataReturn resultBean) {
                        if (resultBean.data != null) {
                            for (CategoryTreeDataReturn.DataBean bean : resultBean.data) {
                                //根据ID检索行业分类
                                if (!TextUtils.isEmpty(categoryid) && bean.id.equals(categoryid)) {
                                    //行业分类
                                    if (!TextUtils.isEmpty(bean.name)) {
                                        tvJichushujuYingyehao.setText(bean.name);
                                    } else {
                                        tvJichushujuYingyehao.setText("");
                                    }
                                }
                                //根据ID检索行业分类 子列表里检索行业分类名称
                                if (bean.children != null) {
                                    for (CategoryTreeDataReturn.DataBean.ChildrenBean childrenBean : bean.children) {
                                        if (!TextUtils.isEmpty(categoryid) && childrenBean.id.equals(categoryid)) {
                                            //行业分类
                                            if (!TextUtils.isEmpty(childrenBean.name)) {
                                                tvJichushujuYingyehao.setText(childrenBean.name);
                                            } else {
                                                tvJichushujuYingyehao.setText("");
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }

                    @Override
                    protected void onMyError(String errorMessage) {
                        showToast(errorMessage + "");
                    }
                });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @OnClick({R.id.back, R.id.ll_jichushuju_dizhi, R.id.ll_jichushuju_dianhua})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                JumpHelper.finish(mContext);
                break;
            case R.id.ll_jichushuju_dizhi:
                if (!TextUtils.isEmpty(lng) && !TextUtils.isEmpty(lat)) {
                    Intent dizhiIntent = new Intent(QiYeXinXiActivity.this, LocationDemo4.class);
                    dizhiIntent.putExtra("lat", lat);
                    dizhiIntent.putExtra("lng", lng);
                    dizhiIntent.putExtra("address", address);
                    JumpHelper.jump(QiYeXinXiActivity.this, dizhiIntent, false);
                } else {
//                    showToast("经纬度为空");
                    LogUtils.e("经纬度为空");
                }
                break;
            case R.id.ll_jichushuju_dianhua:
                call();
                break;
        }
    }

    /**
     * 打电话
     */
    private void call() {
        String tel = tvJichushujuDianhua.getText().toString();
        if (!TextUtils.isEmpty(tel)) {
            Intent intent = new Intent(Intent.ACTION_DIAL);
            Uri data = Uri.parse("tel:" + tel);
            intent.setData(data);
            startActivity(intent);
        }
    }
}
